﻿using System;
using System.Collections.Generic;
using System.Text;

using E.BLL;
using E.Model;
using E.Web.UI.Page;
using E.Enum;

using X.Common;

namespace E.Web.Teacher.internals.exam
{
    public partial class View : TeacherBasePage
    {
        public int id;
        public int cid;
        public ExamInfoModel model = new ExamInfoModel();
        public IList<DictAreaModel> areaList;
        public IList<ExamSettingModel> settingList;
        public IList<QuestionModel> questionList;
        public string certificateName = "";
        public string settingHtml = "";

        public string area_v;

        protected override void ShowPage()
        {
            id = XRequest.GetInt("id", -1);
            cid = XRequest.GetInt("cid", -1);

            if (id != -1)
            {
                EnumExamCategory categoryid = (EnumExamCategory)cid;
                ExamBLL bll = new ExamBLL();
                QuestionBLL bllQuestion = new QuestionBLL();
                model = bll.GetExamInfo(id, categoryid);

                if (model.IsRandom == 0)
                {
                    IList<int> listQuestion = Convert2IList(model.QuestionIDArray);
                    if (listQuestion.Count > 0)
                    {
                        questionList = bllQuestion.GetQuestionDetailInfoList(listQuestion);
                        settingHtml = GetSettingExamHtml(questionList, model.CertificateID);
                    }
                }
                else
                {
                    settingList = bll.GetExamQuestionSetting(id);
                }

                areaList = GetTeacherAreaList(teacherid);
                area_v = Convert2String(model.AreaList);

                switch (model.CategoryID)
                {
                    case EnumExamCategory.CourseExam:
                        certificateName = (new CourseBLL()).GetCertificateNameByID(model.CertificateID);
                        break;
                    default:
                        break;
                }
            }
            else
            {
                ExecuteJS("alert('服务器忙，请稍后再试'); window.location = '/internals/exam/List.aspx';");
            }
        }
        private IList<int> Convert2IList(string parms)
        {
            IList<int> rst = new List<int>();
            string[] parmsArr = parms.Split(',');
            foreach (string parm in parmsArr)
            {
                if (parm != "")
                {
                    int item = Convert.ToInt32(parm);
                    rst.Add(item);
                }
            }
            return rst;
        }
        private string GetSettingExamHtml(IList<QuestionModel> questionList, int iCertificateID)
        {
            StringBuilder sb = new StringBuilder();
            IList<QuestionModel> tmplist = new List<QuestionModel>();
            IList<QuestionModel> tmplist_re = new List<QuestionModel>();
            if (iCertificateID > 0)
            {
                Dictionary<int, string> categoryDic = GetCategoryList(questionList, null, true);
                if (categoryDic.Count > 0)
                {
                    #region 课程测验
                    foreach (KeyValuePair<int, string> kvp in categoryDic)
                    {
                        GetQuestionIDList(questionList, null, kvp.Key, ref tmplist, ref tmplist_re, true);
                        string divID = "div_" + kvp.Key;
                        string imgID = "img_" + kvp.Key;
                        sb.Append("<div class=\"data_rows_left\">\r\n");
                        sb.Append("<div class=\"d_r_qt\"><img id=" + imgID + " onclick=\"SetDisplayExamSetting('" + divID + "','" + imgID + "')\" src=\"../../images/icon/arrow_right.gif\" />" + kvp.Value + "</div>\r\n");
                        sb.Append("</div>\r\n");
                        sb.Append("<div id=" + divID + ">\r\n");
                        if (tmplist_re != null)
                        {
                            if (tmplist_re.Count > 0)
                            {

                                string divRequireID = "divinner" + divID;
                                string imgRequireID = "imginner" + imgID;

                                sb.Append("<div class=\"data_rows_left\">\r\n");
                                sb.Append("<div  class=\"d_r_qt\">&nbsp;&nbsp;<img id=" + imgRequireID + " onclick=\"SetDisplayExamSetting('" + divRequireID + "','" + imgRequireID + "')\" src=\"../../images/icon/arrow_right.gif\" />必选题</div>\r\n");
                                sb.Append("</div>\r\n");

                                sb.Append("<div id=" + divRequireID + "  style=\"display:none\">\r\n");
                                for (int i = 0; i < tmplist_re.Count; i++)
                                {
                                    sb.Append("<div class=\"data_rows_left\">\r\n");
                                    sb.Append("<div class=\"d_r_qx\">&nbsp;&nbsp;" + tmplist_re[i].Title + "</div>\r\n");
                                    sb.Append("</div>\r\n");
                                }
                                sb.Append("</div>\r\n");

                            }
                        }
                        if (tmplist != null)
                        {
                            if (tmplist.Count > 0)
                            {

                                string divNoRequireID = "divnorequire" + divID;
                                string imgNoRequireID = "imgnorequire" + imgID;

                                sb.Append("<div class=\"data_rows_left\">\r\n");
                                sb.Append("<div  class=\"d_r_qt\">&nbsp;&nbsp;<img id=" + imgNoRequireID + " onclick=\"SetDisplayExamSetting('" + divNoRequireID + "','" + imgNoRequireID + "')\" src=\"../../images/icon/arrow_right.gif\" />普通题</div>\r\n");
                                sb.Append("</div>\r\n");

                                sb.Append("<div id=" + divNoRequireID + "  style=\"display:none\">\r\n");
                                for (int i = 0; i < tmplist.Count; i++)
                                {
                                    sb.Append("<div class=\"data_rows_left\">\r\n");
                                    sb.Append("<div class=\"d_r_qx\">&nbsp;&nbsp;" + tmplist[i].Title + "</div>\r\n");
                                    sb.Append("</div>\r\n");
                                }
                                sb.Append("</div>\r\n");
                            }
                        }
                        sb.Append("</div>\r\n");
                    }
                    #endregion
                }
            }
            else
            {
                #region 综合考试
                List<string> courseList = new List<string>();
                foreach (QuestionModel questionItem in questionList)
                {
                    if (!courseList.Contains(questionItem.CourseName))
                    {
                        courseList.Add(questionItem.CourseName);
                    }
                }
                if (courseList.Count > 0)
                {
                    for (int k = 0; k < courseList.Count; k++)
                    {
                        sb.Append("<div class=\"data_rows\">" + courseList[k] + "</div>\r\n");
                        Dictionary<int, string> categoryDic = GetCategoryList(questionList, courseList[k], false);

                        foreach (KeyValuePair<int, string> kvp in categoryDic)
                        {
                            GetQuestionIDList(questionList, courseList[k], kvp.Key, ref tmplist, ref tmplist_re, false);
                            string divID = "div_" + kvp.Key + "_" + k.ToString();
                            string imgID = "img_" + kvp.Key + "_" + k.ToString();
                            sb.Append("<div class=\"data_rows_left\">\r\n");
                            sb.Append("<div class=\"d_r_qt\"><img id=" + imgID + " onclick=\"SetDisplayExamSetting('" + divID + "','" + imgID + "')\" src=\"../../images/icon/arrow_right.gif\" />" + kvp.Value + "</div>\r\n");
                            sb.Append("</div>\r\n");
                            sb.Append("<div id=" + divID + ">\r\n");
                            if (tmplist_re != null)
                            {
                                if (tmplist_re.Count > 0)
                                {

                                    string divRequireID = "divinner_" + divID + "_" + k.ToString();
                                    string imgRequireID = "imginner_" + imgID + "_" + k.ToString();

                                    sb.Append("<div class=\"data_rows_left\">\r\n");
                                    sb.Append("<div  class=\"d_r_qt\">&nbsp;&nbsp;<img id=" + imgRequireID + " onclick=\"SetDisplayExamSetting('" + divRequireID + "','" + imgRequireID + "')\" src=\"../../images/icon/arrow_right.gif\" />必选题</div>\r\n");
                                    sb.Append("</div>\r\n");

                                    sb.Append("<div id=" + divRequireID + "  style=\"display:none\">\r\n");
                                    for (int i = 0; i < tmplist_re.Count; i++)
                                    {
                                        sb.Append("<div class=\"data_rows_left\">\r\n");
                                        sb.Append("<div class=\"d_r_qx\">&nbsp;&nbsp;" + tmplist_re[i].Title + "</div>\r\n");
                                        sb.Append("</div>\r\n");
                                    }
                                    sb.Append("</div>\r\n");

                                }
                            }
                            if (tmplist != null)
                            {
                                if (tmplist.Count > 0)
                                {

                                    string divNoRequireID = "divnorequire_" + divID + "_" + k.ToString();
                                    string imgNoRequireID = "imgnorequire_" + imgID + "_" + k.ToString();

                                    sb.Append("<div class=\"data_rows_left\">\r\n");
                                    sb.Append("<div  class=\"d_r_qt\">&nbsp;&nbsp;<img id=" + imgNoRequireID + " onclick=\"SetDisplayExamSetting('" + divNoRequireID + "','" + imgNoRequireID + "')\" src=\"../../images/icon/arrow_right.gif\" />普通题</div>\r\n");
                                    sb.Append("</div>\r\n");

                                    sb.Append("<div id=" + divNoRequireID + "  style=\"display:none\">\r\n");
                                    for (int i = 0; i < tmplist.Count; i++)
                                    {
                                        sb.Append("<div class=\"data_rows_left\">\r\n");
                                        sb.Append("<div class=\"d_r_qx\">&nbsp;&nbsp;" + tmplist[i].Title + "</div>\r\n");
                                        sb.Append("</div>\r\n");
                                    }
                                    sb.Append("</div>\r\n");
                                }
                            }
                            sb.Append("</div>\r\n");
                        }
                    }
                }
                #endregion
            }
            return sb.ToString();
        }
        private Dictionary<int, string> GetCategoryList(IList<QuestionModel> questionList, string courseName, bool bIsCourseExam)
        {
            Dictionary<int, string> categoryDic = new Dictionary<int, string>();
            foreach (QuestionModel questionItem in questionList)
            {

                if (!categoryDic.ContainsKey(questionItem.CategoryID))
                {
                    if (bIsCourseExam)
                    {
                        categoryDic.Add(questionItem.CategoryID, questionItem.CategoryName);
                    }
                    else
                    {
                        if (questionItem.CourseName == courseName)
                        {
                            categoryDic.Add(questionItem.CategoryID, questionItem.CategoryName);
                        }
                    }
                }
            }
            return categoryDic;
        }
        private IList<QuestionModel> GetQuestionIDList(IList<QuestionModel> questionList, string courseName, int categoryid, ref IList<QuestionModel> tmplist, ref IList<QuestionModel> tmprelist, bool bIsCourseExam)
        {
            tmplist.Clear();
            tmprelist.Clear();
            foreach (QuestionModel questionItem in questionList)
            {
                if (bIsCourseExam)
                {
                    if (questionItem.CategoryID == categoryid && questionItem.IsRequired == false)
                    {
                        tmplist.Add(questionItem);
                    }
                    else if (questionItem.CategoryID == categoryid && questionItem.IsRequired == true)
                    {
                        tmprelist.Add(questionItem);
                    }
                }
                else
                {
                    if (questionItem.CategoryID == categoryid && questionItem.CourseName == courseName && questionItem.IsRequired == false)
                    {
                        tmplist.Add(questionItem);
                    }
                    else if (questionItem.CategoryID == categoryid && questionItem.CourseName == courseName && questionItem.IsRequired == true)
                    {
                        tmprelist.Add(questionItem);
                    }
                }

            }
            return tmplist;
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
